package com.SmartCare.service.impl.homeInfo;

import com.SmartCare.common.dto.homeInfo.AppreciateDTO;
import com.SmartCare.pojo.homeInfo.CareHomeDetails;
import com.SmartCare.service.homeInfo.DetailsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.SmartCare.common.enums.Constants;
import com.SmartCare.common.Result;
import com.SmartCare.common.exception.ServiceException;
import com.SmartCare.service.homeInfo.AppreciateService;
import com.SmartCare.pojo.homeInfo.CareHomeAppreciate;
import com.SmartCare.mapper.homeInfo.CareHomeAppreciateMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 养老院评价信息表 服务实现类
 */
@Service
public class AppreciateServiceImpl extends ServiceImpl<CareHomeAppreciateMapper, CareHomeAppreciate> implements AppreciateService {
    @Resource
    private DetailsService detailsService;
    /*
        "很不满意", //0
        "较不满意", //1
        "一般满意", //2
        "比较满意", //3
        "非常满意"  //4
     */

    /**
     * 查询评价信息
     * @param id 评价信息id
     */
    @Override
    public Result getAppreciate(Long id) {
        QueryWrapper<CareHomeAppreciate> qw = new QueryWrapper<>();
        qw.eq("id", id);
        CareHomeAppreciate one = getOne(qw);
        if (one == null) {
            throw new ServiceException(Constants.CODE_400, "不存在对应的养老院评价");
        }
        return Result.success(one);
    }

    /**
     * 添加评价等级
     */
    @Override
    public Result addAppreciate(AppreciateDTO info) {
        LambdaQueryWrapper<CareHomeDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CareHomeDetails::getName,info.getCareHomeName());
        CareHomeAppreciate one = new CareHomeAppreciate();
        BeanUtils.copyProperties(info,one,"careHomeId");
        one.setCareHomeId(detailsService.getOne(wrapper).getId());

        boolean res = save(one);
        if (res){
            return Result.success("评论成功");
        }
        return Result.error("评论失败");
    }
}
